package com.imay.live.activity.family;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.R;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.family.controller.FamilyController;
import com.imay.live.model.FamilyGroup;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.ResultCode;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;

/**
 * Created by Administrator on 2017/8/29 0029.
 */

public class FamilyActivity extends BaseRequestActivity {

    private FamilyController controler;

    private int count = 15;
    private String requestSuggestCommand;

    /**
     * @param activity
     */
    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, FamilyActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_family;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(R.string.str_family_title);
        controler = (FamilyController) FamilyController.Builder.create(this, FamilyController.class).build();
        refreshLoad();
    }

    private void refreshLoad() {
        requestSuggestCommand = ProtoRequestHelper.task(ProtoHttpFactory.suggestFamily(0, count)).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        super.onRequestSuccess(command, data);
        if (command == requestSuggestCommand) {
            try {
                Result<FamilyGroup> result = JSON.parseObject(data, new TypeReference<Result<FamilyGroup>>() {
                }.getType());
                if (result.result == ResultCode.CodeSuccess && result.data != null) {
                    controler.setNewData(result.data.getFamilys());
                }
            } catch (Exception e) {

            }
        }
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        super.onRequestError(command, code, message);
        ErrorResult.toastError(this, code);
    }
}
